<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.postId ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="请求pk" prop="requestPk">
          <el-input v-model="dataForm.requestPk" placeholder="请求pk"></el-input>
      </el-form-item>
          <el-form-item label="托盘条码" prop="contNo">
          <el-input v-model="dataForm.contNo" placeholder="托盘条码"></el-input>
      </el-form-item>
          <el-form-item label="托盘类型" prop="contType">
          <el-input v-model="dataForm.contType" placeholder="托盘类型"></el-input>
      </el-form-item>
          <el-form-item label="优先级" prop="trkPrty">
          <el-input v-model="dataForm.trkPrty" placeholder="优先级"></el-input>
      </el-form-item>
          <el-form-item label="任务类型" prop="trkType">
          <el-input v-model="dataForm.trkType" placeholder="任务类型"></el-input>
      </el-form-item>
          <el-form-item label="起始点" prop="frmPos">
          <el-input v-model="dataForm.frmPos" placeholder="起始点"></el-input>
      </el-form-item>
          <el-form-item label="目标点" prop="toPos">
          <el-input v-model="dataForm.toPos" placeholder="目标点"></el-input>
      </el-form-item>
          <el-form-item label="当前位置" prop="curPos">
          <el-input v-model="dataForm.curPos" placeholder="当前位置"></el-input>
      </el-form-item>
          <el-form-item label="任务状态" prop="trkSt">
          <el-input v-model="dataForm.trkSt" placeholder="任务状态"></el-input>
      </el-form-item>
          <el-form-item label="是否空托盘" prop="isFull">
          <el-input v-model="dataForm.isFull" placeholder="是否空托盘"></el-input>
      </el-form-item>
          <el-form-item label="任务组号" prop="groupNo">
          <el-input v-model="dataForm.groupNo" placeholder="任务组号"></el-input>
      </el-form-item>
          <el-form-item label="结果码" prop="ackCode">
          <el-input v-model="dataForm.ackCode" placeholder="结果码"></el-input>
      </el-form-item>
          <el-form-item label="结果" prop="ackMsg">
          <el-input v-model="dataForm.ackMsg" placeholder="结果"></el-input>
      </el-form-item>
              <el-form-item label="更新用户" prop="updater">
          <el-input v-model="dataForm.updater" placeholder="更新用户"></el-input>
      </el-form-item>
          <el-form-item label="更新日期" prop="updateDate">
          <el-input v-model="dataForm.updateDate" placeholder="更新日期"></el-input>
      </el-form-item>
          <el-form-item label="备注" prop="comments">
          <el-input v-model="dataForm.comments" placeholder="备注"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        postId: '',
        requestPk: '',
        contNo: '',
        contType: '',
        trkPrty: '',
        trkType: '',
        frmPos: '',
        toPos: '',
        curPos: '',
        trkSt: '',
        isFull: '',
        groupNo: '',
        ackCode: '',
        ackMsg: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: '',
        comments: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        requestPk: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        contNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        contType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        trkPrty: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        trkType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        frmPos: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        toPos: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        curPos: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        trkSt: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        isFull: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        groupNo: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ackCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ackMsg: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updater: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updateDate: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        comments: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.postId) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/wms/post-wcs/${this.dataForm.postId}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.postId ? 'post' : 'put']('/wms/post-wcs/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 1000,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
